package com.wmx.datastructuresandalgorithms;


import java.util.Stack;

public class SingleLinkedList2 {
    private int size;//链表节点数
    private Node head; //头节点、
    public SingleLinkedList2(){
        this.size = 0;
        this.head =null;
    }



    //链表的每个jied
    class Node{
        private Object data;
        private Node next ;
        Node(Object data){
            this.data = data;
        }

    }

    //重头部添加节点
    private Object addNode(Object object){
        Node newNode = new Node(object);
        if(size==0){
           head = newNode;
        }else {
            newNode.next =head;
            head = newNode;

        }

        size++;
        return object;
    }


    //从尾部添加节点
    private Object addRearNode(Object object){
        Node newNode = new Node(object);
        Node temp = head;
        if(size==0){
            head = newNode;
        }else {
           while (true){
               if(temp.next == null){
                   break;
               }
             temp =  temp.next;
           }

           temp.next = newNode;

        }
        size++;
        return object;
    }
    //查看头节点

    public Node getHead() {
        return head;
    }

    //删除节点
    public void  remove(){
       if(head == null)
           throw new  IllegalArgumentException("链表为空");

       Node newNode =head;
       while (newNode.next != null){
           if(newNode.next.next==null){
               break;
           }
           newNode = head.next;
       }

       newNode.next=null;
       size--;
    }


    //单向链表反转

    /**
     * 思路：
     * ①定义一个新头
     * ②：从头到尾遍历链表，并将其节点放在从头部插入。
     * @param headNode
     */
    public void reversal(Node headNode){
        if(headNode == null || headNode.next==null){
            return;
        }
        //需要反转的链表头
        Node newNode = headNode;

        Node cur = null;

        //新头
        Node newHead = null;


        while (true){

             cur = newNode.next;
             newNode.next = newHead;
             newHead = newNode;
             newNode = cur;
            if(newNode.next == null){
                cur.next = newHead;
                newHead = cur;
                break;
            }
        }
        head = newHead;
    }





    public  void  display(Node headNode){
        System.out.printf("[");
        while (true){
            if(headNode.next == null){
                System.out.printf(headNode.data+"]");
                break;
            }else {
                System.out.printf(headNode.data+"—>");
            }
            headNode = headNode.next;
        }

    }


    public void del(Node headNode){
        //①对链表头的校验 判断头是否为空 或 头是否包含下一个节点
        // ②：遍历链表，判断节点是否为尾节点，将尾巴的上一个节点的next指向null
        if(headNode == null){
            System.out.printf("链表为空");
        }

        while (headNode.next != null){
            if(headNode.next.next == null){
                headNode.next =null;
                break;
            }
            headNode = headNode.next;
        }

    }

    public static void main(String[] args) {
        SingleLinkedList2 singleLinkedList = new SingleLinkedList2();
        singleLinkedList.addRearNode(2);
        singleLinkedList.addRearNode(44);
        singleLinkedList.addRearNode(21);
        singleLinkedList.addRearNode(13);
        singleLinkedList.display(singleLinkedList.getHead());

       singleLinkedList.reversal(singleLinkedList.getHead());
       singleLinkedList.display(singleLinkedList.getHead());

        singleLinkedList.del(singleLinkedList.getHead());

        singleLinkedList.del(singleLinkedList.getHead());

        singleLinkedList.del(singleLinkedList.getHead());

        singleLinkedList.del(singleLinkedList.getHead());


        singleLinkedList.display(singleLinkedList.getHead());


       /* String a ="Aa";
        String b ="BB";
        System.out.println("+++++++++++++"+a.hashCode());
        System.out.println("+++++++++++++"+b.hashCode());*/
      //  对于字符串"Aa",hash = 0+65 + 31*65 + 97 = 2112

       // 对于字段串"BB"，hash = 0+66 + 31*66 +66 = 2112;

      
//        String a1 ="吴梦鑫";
//        String b1 ="吴梦鑫";
//        Integer a =20;
//        System.out.println(a.hashCode());
//
//
//        char val[] = b1.toCharArray();
//        for (int i= 0;i<=val.length-1;i++){
//            System.out.println(val[i]);
//        }
//        System.out.println("============"+val.toString());
//        System.out.println("a1+++++++++++++"+a1.hashCode());
//        System.out.println("b1+++++++++++++"+b1.hashCode());
//        System.out.println('A'==65);


    }
}
